package cn.jxau.fukin.service.impl;

import static cn.jxau.fukin.util.ConstantUtil.SUCCESS;
import static cn.jxau.fukin.util.PKUtil.getPrimaryKey;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jxau.fukin.dao.NoticeMapper;
import cn.jxau.fukin.model.Admin;
import cn.jxau.fukin.model.Notice;
import cn.jxau.fukin.model.NoticeExample;
import cn.jxau.fukin.model.NoticeExample.Criteria;
import cn.jxau.fukin.service.INoticeService;

@Service("noticeService")
public class NoticeService implements INoticeService {

	@Autowired
	NoticeMapper noticeMapper;

	// 添加公告
	public String addNotice(Admin admin, Notice notice) {
		if (notice == null || admin == null) {
			return null;
		}
		notice.setNoticeId(getPrimaryKey());
		notice.setaId(admin.getaId());
		noticeMapper.insert(notice);
		return SUCCESS;
	}

	// 更新公告
	public String updateNotice(Admin admin, Notice notice) {
		if (notice == null || admin == null) {
			return null;
		}
		notice.setaId(admin.getaId());
		noticeMapper.updateByPrimaryKey(notice);
		return SUCCESS;
	}

	// 删除公告
	public String deleteNotice(String noticeId) {
		noticeMapper.deleteByPrimaryKey(noticeId);
		return SUCCESS;
	}

	public List<Notice> selectNotice() {
		return noticeMapper.selectAllNotice();
	}

	// 按条件查询公告
	public List<Notice> selectNotice(Notice notice) {
		if (notice == null)
			return null;
		NoticeExample noticeExample = new NoticeExample();
		Criteria criteria = noticeExample.createCriteria();
		judge(notice, criteria);
		return noticeMapper.selectByExample(noticeExample);
	}

	// 根据主键查询公告
	public Notice selectNiticeByPrimaryKey(String noticeId) {
		return noticeMapper.selectByPrimaryKey(noticeId);
	}

	void judge(Notice notice, Criteria criteria) {
		if (notice.getaId() != null && !"".equals(notice.getaId())) {
			criteria.andAIdEqualTo(notice.getaId());
		}
		if (notice.getNoticeContent() != null && !"".equals(notice.getNoticeContent().trim())) {
			criteria.andNoticeContentEqualTo(notice.getNoticeContent());
		}
		if (notice.getNoticeId() != null && !"".equals(notice.getNoticeId().trim())) {
			criteria.andNoticeIdEqualTo(notice.getNoticeId());
		}
		if (notice.getNoticeTime() != null) {
			criteria.andNoticeTimeEqualTo(notice.getNoticeTime());
		}
		if (notice.getNoticeTitle() != null && !"".equals(notice.getNoticeTitle().trim())) {
			criteria.andNoticeTitleEqualTo(notice.getNoticeTitle());
		}
	}

}
